package com.capgemini.cn.authority.data.entity;

import com.capgemini.cn.authority.data.core.DefaultIdentityEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.List;

/**
 * <p>权限表</p>
 * Created at 2017/8/23
 *
 * @author Andriy
 */
@Entity
@Table(name = "cg_authorities")
@NoArgsConstructor
@AllArgsConstructor
public class Authorities extends DefaultIdentityEntity {

    /**
     * 权限分类信息
     */
    @ManyToOne(targetEntity = AuthoritiesType.class, optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "type_id", referencedColumnName = "id", nullable = false, updatable = false)
    @Getter
    @Setter
    private AuthoritiesType authoritiesType;

    /**
     * 权限名称
     */
    @Column(name = "authority_name", length = 50, nullable = false)
    @Getter
    @Setter
    private String authorityName;

    /**
     * 权限编码
     */
    @Column(name = "authority_code", length = 50, nullable = false, unique = true)
    @Getter
    @Setter
    private String authorityCode;

    /**
     * 角色与权限的多对多关系，用以获得当前权限所被使用的角色信息
     */
    @ManyToMany(targetEntity = Roles.class, mappedBy = "authorities")
    @Getter
    @Setter
    private List<Roles> roles;

    /**
     * 资源组与权限的多对多关系，用以获得当前权限所被使用的资源组信息
     */
    @ManyToMany(targetEntity = AssertsGroup.class, mappedBy = "authorities")
    @Getter
    @Setter
    private List<AssertsGroup> assertsGroups;
}
